package com.xiong.fan.observer.core;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class EventLisenter {


    //Map相当于是一个注册器
    protected Map<Enum,Event> events = new HashMap<Enum,Event>();

    public void addLisenter(Enum eventType, Object target, Method callMethod){
        events.put(eventType,new Event(target,callMethod,this) );
    }

    protected void trigger(Enum call){
        if(!events.containsKey(call)){
            return;
        }
        trigger(events.get(call).setTrigger(call.toString()));
    }

    private void trigger(Event e) {
        e.setTime(System.currentTimeMillis());
        try {
            e.getCallback().invoke(e.getTarget(),e);
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        }
    }


}
